package com.lc.ibps.platform.rest.org;



import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.org.service.IPartyEmployeeMgrService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 用户管理控制器，第三方系统调用。
 *
 * <pre> 
 * 构建组：ibps-platform-webapi
 * 作者：Lium
 * 邮箱：1316679699@qq.com
 * 日期：2017年7月13日-上午9:37:39
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value="/api/webapi/employeeMgrService")
@Api(value = "/employeeMgrService",tags="用户管理-员工操作服务")
@RestController
public class EmployeeMgrController extends BaseApiController{
	
	
	private IPartyEmployeeMgrService employeeMgrService;
	
	public EmployeeMgrController(){
		employeeMgrService = AppUtil.getBean(IPartyEmployeeMgrService.class);
	}
	
	/**
	 * 添加用户信息
	 *
	 * @param json {id:""/*选填/,account:"test"/*必填/,password:"123"/*必填/,isSuper:"Y"/*必填/,name:"test"/*选填/,
	 * 				status:"actived"/*必填/,gender:"male"/*必填/,email:"123@qq.com"/*必填/,address:"地址"/*选填/,
	 * 				mobile:""/*选填/,qq:""/*选填/,wcAccount:""/*选填/,photo:""/*选填/,groupID:""/*选填/,
	 * 				createTime:""/*选填/,
	 * 				posItemList:[{id:"", name:"", isMainPost:"", isPrincipal:""}]/*选填/,
	 * 				roleItemList:[{id:"", name:"", canDelete:""}]/*选填/,
	 * 				attrItemList:[{attrId:"", value:""}]/*选填/,
	 * 				userGroupItemList:[{groupId:""}]/*选填/}
	 */
	@ApiOperation(value = "添加用户信息", notes = "添加用户信息")
	@RequestMapping(value="/createEmployee",method=RequestMethod.PUT)
	public APIResult<String> createEmployee(
			@ApiParam(value = "用户信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			){		
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.createEmployee(json);
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户信息失败！", e);
		}
		return result;
	}
	
	/**
	 * 修改用户信息
	 *
	 * @param json {id:"498b9030",account:"test"/*必填/,password:"123"/*必填/,isSuper:"Y"/*必填/,name:"test"/*选填/,
	 * 				gender:"male"/*必填/,email:"123@qq.com"/*必填/,address:"地址"/*选填/,mobile:""/*选填/,QQ:""/*选填/,
	 * 				wcAccount:""/*选填/,photo:""/*选填/,groupID:""/*选填/,createTime:"2017-02-09 00:00:00"/*必填/,
	 * 				posItemList:[{id:"", name:"", isMainPost:"", isPrincipal:""}]/*选填/,
	 * 				roleItemList:[{id:"", name:"", canDelete:""}]/*选填/,
	 * 				attrItemList:[{attrId:"", value:""}]/*选填/,
	 * 				userGroupItemList:[{groupId:""}]/*选填/}
	 */
	@ApiOperation(value = "修改用户信息", notes = "修改用户信息")
	@RequestMapping(value="/updateEmployee",method=RequestMethod.POST)
	public APIResult<String> updateEmployee(
			@ApiParam(value = "用户信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			){		
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.updateEmployee(json);
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "修改用户信息失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "添加用户角色", notes = "根据用户ID添加用户角色")
	@RequestMapping(value="/addRole4Employee",method=RequestMethod.PUT)
	public APIResult<String> addRole4Employee(
			@ApiParam(value = "用户ID", required = true) 
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true)
			@RequestParam(name = "roles", required = true) String roles
			) {		
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.addRole4Employee(id, roles);	
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户角色失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "移除用户角色", notes = "根据用户ID移除用户角色")
	@RequestMapping(value="/removeRole4Employee",method=RequestMethod.DELETE)
	public APIResult<String> removeRole4Employee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true)
			@RequestParam(name = "roles", required = true) String roles
			) {			
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.removeRole4Employee(id, roles);
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除用户角色失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "添加用户岗位", notes = "根据用户ID添加用户岗位")
	@RequestMapping(value="/addPosition4Employee",method=RequestMethod.PUT)
	public APIResult<String> addPosition4Employee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "岗位ID", required = true)
			@RequestParam(name = "positions", required = true) String positions
			) {		
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.addPosition4Employee(id, positions);	
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户岗位失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "移除用户岗位", notes = "根据用户ID用户岗位")
	@RequestMapping(value="/removePosition4Employee",method=RequestMethod.DELETE)
	public APIResult<String> removePosition4Employee(
			@ApiParam(value = "用户ID", required = true) 
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "岗位ID", required = true) 
			@RequestParam(name = "positions", required = true) String positions
			) {
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.removePosition4Employee(id, positions);
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除用户岗位失败！", e);
		}
		return result;
	}
	
	
	@ApiOperation(value = "添加用户组织", notes = "根据用户ID添加用户组织")
	@RequestMapping(value="/addOrg4Employee",method=RequestMethod.PUT)
	public APIResult<String> addOrg4Employee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "组织ID", required = true)
			@RequestParam(name = "groupID", required = true) String groupID
			) {		
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.addOrg4Employee(id, groupID);	//TODO  没添加成功 没有报错
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户组织失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "移除用户组织", notes = "根据用户ID移除用户组织")
	@RequestMapping(value="/removeOrg4Employee",method=RequestMethod.DELETE)
	public APIResult<String> removeOrg4Employee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "id", required = true) String id
			) {
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.removeOrg4Employee(id);		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除用户组织失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "添加用户组", notes = "根据用户ID添加用户组")
	@RequestMapping(value="/addGroup4Employee",method=RequestMethod.PUT)
	public APIResult<String> addGroup4Employee(
			@ApiParam(value = "用户ID", required = true) 
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户组ID", required = true) 
			@RequestParam(name = "groups", required = true) String groups
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.addGroup4Employee(id, groups);		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加用户组失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "移除用户组", notes = "根据用户ID移除用户组")
	@RequestMapping(value="/removeGroup4Employee",method=RequestMethod.DELETE)
	public APIResult<String> removeGroup4Employee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户组ID", required = true)
			@RequestParam(name = "groups", required = true) String groups
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.removeGroup4Employee(id, groups);		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除用户组失败！", e);
		}
		return result;
	}
	
	/**
	 * 修改用户密码
	 *
	 * @param json {id:"498b9030"/*必填/,primitivePassword:"1"/*必填/,newPassword:"123"/*必填/,isReset:0/*选填/}
	 */

	@ApiOperation(value = "修改用户密码", notes = "根据用户ID修改用户密码")
	@RequestMapping(value="/updatePasswd4Employee",method=RequestMethod.POST)
	public APIResult<String> updatePasswd4Employee(
			@ApiParam(value = "密码信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.updatePasswd4Employee(json);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "修改用户密码失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "删除用户信息", notes = "根据用户ID删除用户信息")
	@RequestMapping(value="/deleteEmployee",method=RequestMethod.DELETE)
	public APIResult<String> deleteEmployee(
			@ApiParam(value = "用户ID", required = true)
			@RequestParam(name = "ids", required = true) String ids
			) {
		APIResult<String> result = new APIResult<>();
		try{
			employeeMgrService.deleteEmployee(ids);		//TODO 网页没有显示   但是数据库有	
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "删除用户信息失败！", e);
		}
		return result;
	}
}
